package reflect;
import java.lang.reflect.Constructor;
/**
 * 使用有参构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws Exception {
        Person p = new Person("范传奇");
        System.out.println(p);
        //获取类对象
        Class cls = Class.forName("reflect.Person");
//        cls.newInstance();//new Person();
//        Constructor c = cls.getConstructor();//Person() 获取无参构造器
//        c.newInstance();
        /*
            java.lang.reflect.Constructor   它的每一个实例用于反映一个类的构造器
         */
        //Person(String)
        Constructor c = cls.getConstructor(String.class);
        //new Person("王克晶");
        Object obj = c.newInstance("王克晶");
        System.out.println(obj);
        //Person(String,int)
        Constructor c2 = cls.getConstructor(String.class, int.class);
        //new Person("王伟超",33)
        Object obj2 = c2.newInstance("王伟超", 33);
        System.out.println(obj2);
    }
}